// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {colliderGroup} from "./HeroContact";
import Music, {musicName} from "../PublicScript/Music";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SpineControl extends cc.Component {

    @property({tooltip: "位移距离"})
    moveDistance: number = 0;

    @property({tooltip: "是否掉落"})
    down: boolean = false;

    //速度
    speed = 300;
    //时间
    time = 0;
    //做存储的对象，保存已经实现运动的物体
    runNode = {};
    //count
    count = 0;

    onLoad () {
        this.time = this.moveDistance/this.speed;
    }

    start () {

    }

    //移动
    moveAnim(node){
        Music.GamePlayEffect(musicName.daodiaoluo);
        node.runAction(
            cc.sequence(
                cc.moveBy(this.time, cc.v2(0, -this.moveDistance)),
                cc.callFunc(()=>{
                    node.active = false;
                    this.remove();
                })
            )
        )
    }

    onBeginContact(contact, self, other) {
        if(this.down == true){
            return;
        }
        if(other.node.group == colliderGroup.hero && !this.runNode[self.tag]){
            this.runNode[self.tag] = 1;
            this.moveAnim(this.node.children[self.tag])
        }
    }

    //消除
    remove(){
        this.count ++;
        if(this.count >= 3){
            this.node.destroy();
        }
    }

    // update (dt) {}
}
